#include "include.h"
#include "edid.h"

u8      g_aedid_buff[EDID_BLOCK_SIZE * 2];
code const u8   EDID_Default[256] = { /*HDMI2.0 */
    0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x4C, 0x2D, 0xB4, 0x0B, 0x01, 0x00, 0x00, 0x00,
    0x02, 0x18, 0x01, 0x03, 0x80, 0x6F, 0x3E, 0x78, 0x0A, 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26,
    0x0F, 0x50, 0x54, 0xBD, 0xEF, 0x80, 0x71, 0x4F, 0x81, 0xC0, 0x81, 0x00, 0x81, 0x80, 0x95, 0x00,
    0xA9, 0xC0, 0xB3, 0x00, 0x01, 0x01, 0x08, 0xE8, 0x00, 0x30, 0xF2, 0x70, 0x5A, 0x80, 0xB0, 0x58,
    0x8A, 0x00, 0x50, 0x1D, 0x74, 0x00, 0x00, 0x1E, 0x02, 0x3A, 0x80, 0x18, 0x71, 0x38, 0x2D, 0x40,
    0x58, 0x2C, 0x45, 0x00, 0x50, 0x1D, 0x74, 0x00, 0x00, 0x1E, 0x00, 0x00, 0x00, 0xFD, 0x00, 0x18,
    0x4B, 0x0F, 0x87, 0x3C, 0x00, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xFC,
    0x00, 0x53, 0x41, 0x4D, 0x53, 0x55, 0x4E, 0x47, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, 0x01, 0x0A,
    0x02, 0x03, 0x49, 0xF1, 0x57, 0x61, 0x10, 0x1F, 0x04, 0x13, 0x05, 0x14, 0x20, 0x21, 0x22, 0x5D,
    0x5E, 0x5F, 0x60, 0x65, 0x66, 0x62, 0x63, 0x64, 0x07, 0x16, 0x03, 0x12, 0x23, 0x09, 0x07, 0x07,
    0x83, 0x01, 0x00, 0x00, 0xE2, 0x00, 0x0F, 0x6E, 0x03, 0x0C, 0x00, 0x40, 0x00, 0xB8, 0x3C, 0x21,
    0x00, 0x80, 0x01, 0x02, 0x03, 0x04, 0x6E, 0xd8, 0x5d, 0xc4, 0x01, 0x78, 0x80, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0xE3, 0x0F, 0x01, 0xE0, 0x01, 0x1D, 0x80, 0xD0, 0x72, 0x1C, 0x16,
    0x20, 0x10, 0x2C, 0x25, 0x80, 0x50, 0x1D, 0x74, 0x00, 0x00, 0x9E, 0x66, 0x21, 0x56, 0xAA, 0x51,
    0x00, 0x1E, 0x30, 0x46, 0x8F, 0x33, 0x00, 0x50, 0x1D, 0x74, 0x00, 0x00, 0x1E, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA
};

bool bReadEdidFromSink( Tx_TypeDef tx )
{
    u8  ucOffsetAddress = 0;
    u8  ucLoopx;
    HDMI_WriteI2C_Byte( 0x02, 0xbf );
    HDMI_WriteI2C_Byte( 0x02, 0xff );                       /*Reset DDC */
    HDMI_WriteI2C_Byte( 0x44, tx );                         /*bit[1:0] = tx_ddc_sel  ,  0=tx0 ; 1=tx1 ; 2=tx2 ; 3=tx3 */
    HDMI_WriteI2C_Byte( 0x41, 0x39 );                       /*gurantee one cycle duration */
    HDMI_WriteI2C_Byte( 0x45, 0xA0 );                       /*device address :  EDID_ADDR = 0xA0 ; SCDC_ADDR = 0xA8 */
    HDMI_WriteI2C_Byte( 0x48, 0x20 );                       /*Max read 32Byte */
    ucOffsetAddress = 0;
    for ( ucLoopx = 0; ucLoopx < 8; ucLoopx++ )
    {
        HDMI_WriteI2C_Byte( 0x47, ucOffsetAddress );    /*initial address */
        HDMI_WriteI2C_Byte( 0x44, tx | 0x10 );          /*bit[6:4] = access command ; 1=seq_rd , 2=seq_wr , 7=nop_com */
        HDMI_WriteI2C_Byte( 0x44, tx | 0x70 );          /*bit[6:4] = access command ; 1=seq_rd , 2=seq_wr , 7=nop_com */
        Delay_ms( 100 );
        HDMI_ReadI2C_ByteN( 0x62, &g_aedid_buff[ucLoopx * 0x20], 0x20 );
        ucOffsetAddress += 0x20;                        /*next 32 bytes */
    }
    if ( (g_aedid_buff[0] == 0x00) && (g_aedid_buff[1] == 0xFF) &&
         (g_aedid_buff[2] == 0xFF) && (g_aedid_buff[3] == 0xFF) &&
         (g_aedid_buff[4] == 0xFF) && (g_aedid_buff[5] == 0xFF) &&
         (g_aedid_buff[6] == 0xFF) && (g_aedid_buff[7] == 0x00) )
    {
        printf( "Read EDID OK\n");
        return(SUCCESS);
    }else {
        memcpy( g_aedid_buff, EDID_Default, 256 );
    }
    return(ERROR);
}


void WriteEdidToShadow( void )
{
    u16 count = 0;
    //I2CADR = LT86102UX;
    HDMI_WriteI2C_Byte( 0x20, 0x00 );       /*disable Source read edid */
    for ( count = 0; count < 256; count++ )
    {
        HDMI_WriteI2C_Byte( 0x60, g_aedid_buff[count] );
    }
    HDMI_WriteI2C_Byte( 0x20, 0x01 );       /*enbale Source read edid */
}
